package com.nf.controller.home;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.OrderInfoDao;
import com.nf.dao.impl.OrderInfoDaoImpl;
import com.nf.entity.OrderInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 编写servlet，实现添加订单
 */
@WebServlet("/api/OrderAddServlet.do")
public class OrderAddServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String filmName = req.getParameter("filmName");
        String hallName = req.getParameter("hallName");
        String showDate = req.getParameter("showDate");
        String startTime = req.getParameter("startTime");
        Float price = Float.valueOf(req.getParameter("price"));
        int showId = Integer.parseInt(req.getParameter("showId"));
        int userId = Integer.parseInt(req.getParameter("userId"));
        int filmId = Integer.parseInt(req.getParameter("filmId"));
        String place = req.getParameter("place");

        OrderInfo order = new OrderInfo();
        order.setFilmName(filmName);
        order.setHallName(hallName);
        order.setShowDate(showDate);
        order.setStartTime(startTime);
        order.setPrice(price);
        order.setSeats(place);
        order.setFilmId(filmId);
        order.setShowId(showId);
        order.setUserId(userId);

        OrderInfoDao dao = new OrderInfoDaoImpl();
        int id=dao.add(order);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        out.print(new Gson().toJson(successJson(id)));
        out.flush();
        out.close();
    }
}
